/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.parser;

import java.sql.SQLException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;
import rdfparser.bean.PostBean;

/**
 *
 * @author Amendra Shrestha
 */
public class PostParser extends ParserHandler {

    private String parsedValue;
    private PostBean post;
    private boolean isPost = false;
    private boolean hasMaker = false;
    private boolean replyOf = false;
    private boolean isUser = false;

    /**
     * parses the XML document file, prints the parse data of XML document file
     * and adds the parsed data of XML document file to database
     *
     * @param baseDir
     * @param fileDir
     * @param fileName
     */
    public void parsePost(String fileName) throws SQLException {
            post = new PostBean();
        if (parseDocument(fileName)) {
            //System.out.println("Filename from Parser method: " + fileName);
            System.out.println("\tParsed document successfully.");
            printDocument();
            addData();
        } else {
            System.out.println("\tException occured while parsing document.");
        }
    }

    /**
     * overriding the default SAX start element method parses each starting XML
     * element tag and sets the required extracted data
     *
     * @param uri
     * @param localName
     * @param elementName
     * @param attributes
     */
    @Override
    public void startElement(String uri,
            String localName,
            String elementName,
            Attributes attributes) throws SAXParseException {

        if (elementName.equalsIgnoreCase("sioct:BoardPost")) {            
            post.setPost(attributes.getValue("rdf:about"));
            isPost = true;
        }
        if (elementName.equalsIgnoreCase("rdf:type") && isPost) {
            post.setType(attributes.getValue("rdf:resource"));
        }
        if (elementName.equalsIgnoreCase("sioc:User")) {
            isUser = true;
            post.setUser(attributes.getValue("rdf:about"));
        }
        if (elementName.equalsIgnoreCase("rdfs:seeAlso") && isUser) {
            post.setSeeAlso(attributes.getValue("rdf:resource"));
            isUser = false;
        }
        if (elementName.equalsIgnoreCase("foaf:Person")) {
            hasMaker = true;
            post.setMaker(attributes.getValue("rdf:about"));
        }
        if (elementName.equalsIgnoreCase("rdfs:seeAlso") && hasMaker) {
            post.setMakerSeeAlso(attributes.getValue("rdf:resource"));
            hasMaker = false;
        }
        if (elementName.equalsIgnoreCase("sioc:reply_of")) {
            replyOf = true;
        }
        if (elementName.equalsIgnoreCase("sioc:Post") && replyOf) {
            post.setReplyOfPost(attributes.getValue("rdf:about"));
        }
        if (elementName.equalsIgnoreCase("rdfs:seeAlso") && replyOf) {
            post.setReplyOfseeAlso(attributes.getValue("rdf:resource"));
            replyOf = false;
        }
    }

    /**
     * overriding the default SAX end element method parses each ending XML
     * element tag and sets the required extracted data
     *
     * @param namespaceURI
     * @param localName
     * @param elementName
     */
    @Override
    public void endElement(String namespaceURI,
            String localName,
            String elementName) {
        if (elementName.equalsIgnoreCase("dc:title") && isPost) {
            post.setTitle(parsedValue);
            isPost = false;
        }
        if (elementName.equalsIgnoreCase("dcterms:created")) {
            post.setCreated(parsedValue);
        }
        if (elementName.equalsIgnoreCase("sioc:content")) {
            post.setContent(parsedValue);
        }
    }

    /**
     * holds extracted text data in string object
     *
     * @param ch
     * @param start
     * @param length
     * @throws SAXException
     */
    @Override
    public void characters(char ch[],
            int start,
            int length) {
        parsedValue = new String(ch, start, length);
    }

    /**
     * prints parsed data of XML document
     */
    public void printDocument() {

            if (post.getPost() != null) {
                System.out.println("Posts: " + post.getPost());
                System.out.println("Type: " + post.getType());
                System.out.println("Title: " + post.getTitle());
                System.out.println("User: " + post.getUser());
                System.out.println("seeAlso: " + post.getSeeAlso());
                System.out.println("Maker: " + post.getMaker());
                System.out.println("MakerseeAlso: " + post.getMakerSeeAlso());
                System.out.println("Created: " + post.getCreated());
                System.out.println("Content: " + post.getContent());
                System.out.println("Reply Of Post: " + post.getReplyOfPost());
                System.out.println("ReplyOfseeAlso: " + post.getReplyOfseeAlso());
            } else {
                System.out.println("There is nothing to print");
            }
    }

    /**
     * adds parsed data of XML document in db
     */
    private void addData() throws SQLException {
        System.out.println("Adding data....");
        post.addPosts();
        System.out.println("Successfully added....");
    }
}
